package com.deeper.customview.ui.c8.demo.c08d02;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Point;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;

import com.deeper.customview.R;

public class SlidingMenu extends HorizontalScrollView {
    private final int leftPaddingWidth;
    private boolean once = false;
    private boolean isOpen = false;


    public SlidingMenu(Context context) {
        this(context, null, 0);
    }


    public SlidingMenu(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }


    public SlidingMenu(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.SlidingMenu);
        leftPaddingWidth = typedArray.getDimensionPixelSize(R.styleable.SlidingMenu_left_padding_width,
                (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 150,
                        getResources().getDisplayMetrics()));
        typedArray.recycle();
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        once = true;
        //水平滚动View只能有一个直接子组件
        LinearLayout subView = (LinearLayout) this.getChildAt(0);
        //指定侧边栏和主界面的宽
        //第0个子组件就是侧边栏，第一个子组件就是主界面
        LinearLayout slidingMenu = (LinearLayout) subView.getChildAt(0);
        ViewGroup content = (ViewGroup) subView.getChildAt(1);
        slidingMenu.getLayoutParams().width = leftPaddingWidth;
        content.getLayoutParams().width = this.getScreenWidth();
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }


    private int getScreenWidth() {
        WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
        Point point = new Point();
        wm.getDefaultDisplay().getSize(point);
        return point.x;
    }


    /**
     * 初始状态下在该方法中隐藏侧边栏
     */
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        if (once) {
            this.scrollTo(leftPaddingWidth, 0);
        }
        once = false;
    }


    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        //当手指松开的时候，决定侧边栏是隐藏还是显示
        //如果侧边栏滚出屏幕的宽度大于侧边栏的一半，则隐藏侧边栏
        //如果侧边栏滚出屏幕的宽度小于等于侧边栏的一半，则显示侧边栏
        if (ev.getAction() == MotionEvent.ACTION_UP) {
            int dx = this.getScrollX();
            int halfWidth = this.leftPaddingWidth / 2;
            if (dx < halfWidth) {
                //显示侧边栏
                this.smoothScrollTo(0, 0);
                this.isOpen = true;
            } else {
                this.smoothScrollTo(leftPaddingWidth, 0);
                this.isOpen = false;
            }
            return true;
        }
        return super.onTouchEvent(ev);
    }


    public void toggle() {
        if (isOpen) {
            hide();
        } else {
            open();
        }
    }


    private void hide() {
        if (isOpen) {
            this.smoothScrollTo(leftPaddingWidth, 0);
            isOpen = false;
        }
    }


    private void open() {
        if (!isOpen) {
            this.smoothScrollTo(0, 0);
            isOpen = true;
        }
    }
}
